package people.paper.tv.activity.home.modual.sptt.view.impl;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.skyworth.ui.api.SkyPlayingView;
import com.skyworth.ui.newrecycleview.NewRecycleAdapterItem;
import com.skyworth.util.Util;

import people.paper.tv.R;
import people.paper.tv.activity.home.modual.sptt.view.data.VideoData;
import people.paper.tv.utils.ui.MarqueeTextView;

public class HeadlineItemView extends FrameLayout implements NewRecycleAdapterItem<VideoData> {
    FrameLayout contentLayout;
    MarqueeTextView title;
    SkyPlayingView playingView;
    VideoData mdata;
    int curPosition;

    public HeadlineItemView(@NonNull Context context) {
        super(context);

        setFocusable(true);
        setFocusableInTouchMode(true);

        contentLayout = new FrameLayout(context);
        addView(contentLayout, new LayoutParams(Util.Div(636), Util.Div(120)));

        LayoutParams play_params = new LayoutParams(Util.Div(24), Util.Div(24), Gravity.CENTER_VERTICAL);
        play_params.leftMargin = Util.Div(12);
        playingView = new SkyPlayingView(context);
        contentLayout.addView(playingView, play_params);
        playingView.setVisibility(GONE);

        title = new MarqueeTextView(context);
        title.setTextSize(Util.Dpi(36));
        title.setTextColor(getResources().getColor(R.color.title_unfocus));
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL);
        params.leftMargin = Util.Div(60);
        params.rightMargin = Util.Div(60);
        contentLayout.addView(title, params);
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void onUpdateData(VideoData data, int position) {
        this.mdata = data;
        curPosition = position;
        if (data == null) {
            return;
        }
        refreshUI();
    }

    @Override
    public void clearItem() {

    }

    @Override
    public void refreshUI() {
        if (mdata != null) {
            title.setText(mdata.title);
            onFocusChange(hasFocus());
        }
    }

    public void onClickAction() {
        if(mdata != null)
            mdata.onPlay.run();
    }

    public void onFocusChange(boolean hasFocus) {
        Log.i("SPTTViewImpl onNewsLoad", "SPTTViewImpl refreshCurPlayId onFocusChange:"+curPosition
                +"; " + mdata.isCurPlay+"; "+hasFocus+"; "+mdata.isCurSelected);
        title.setSelected(hasFocus);
        if(hasFocus){
            setBackgroundColor(getResources().getColor(R.color.content_selected_bg));
            title.setTextColor(getResources().getColor(R.color.title_focus));
        }else {
            setBackground(null);
            title.setTextColor(mdata.isCurSelected?getResources().
                    getColor(R.color.title_selected):getResources().getColor(R.color.title_unfocus));
        }
        setAnim();
    }

    public void setAnim(){
        Log.i("SPTTViewImpl onNewsLoad", "SPTTViewImpl refreshCurPlayId setAnim:"+curPosition
                +"; " + mdata.isCurPlay+"; "+hasFocus()+"; "+mdata.isCurSelected);
        if(hasFocus()){
            playingView.setmColor(getResources().getColor(R.color.title_focus));
        }else {
            playingView.setmColor(getResources().getColor(R.color.title_selected));
        }
        if(mdata.isCurPlay){
            playingView.setVisibility(VISIBLE);
            playingView.startAnim();
        }else if(mdata.isCurSelected){
            playingView.setVisibility(VISIBLE);
            playingView.post(new Runnable() {
                @Override
                public void run() {
                    playingView.stopAnim();
                }
            });
        }else {
            playingView.post(new Runnable() {
                @Override
                public void run() {
                    playingView.stopAnim();
                }
            });
            playingView.setVisibility(GONE);
        }
    }

    @Override
    public void destroy() {

    }
}
